
#ifndef CONDNOTICE_H
#define CONDNOTICE_H
#include <Arduino.h>
#include <String.h>
#include "List.h"
#include "utils.h"
#include "NTPTime.h"
#include "ArduinoJson.h"

#define MAX_CONDS_NUM 5
class Conds
{
public:
    String id;
    String logic;
    String sensorValue;
    String name;
    String unit;
    float triggleValue;

public:
    String toString()
    {
        printStr("ttttt", triggleValue);
        String res ;
        if(unit.equals("%")){
        this->triggleValue = this->triggleValue*100;
        String str  =   String( this->triggleValue,0);//舍弃小数点后面的数

        if(str.indexOf('.')>=0){
           str = str.substring(0,str.indexOf('.'));
        }

        res = this->name + this->logic + this->sensorValue + this->unit + String(",当前值：") +str+this->unit;
        return res;
       
        } 
        res = this->name + this->logic + this->sensorValue + this->unit + String(",当前值：") + String(this->triggleValue)+this->unit;
        return res;
    }

    bool isMeet(float outValue)
    {
        float value=0;
        if(this->unit.equals("%")){
          value = sensorValue.toFloat()/100;
        }else{
            value = sensorValue.toFloat();
        }
       // printStr("sensorValue", value);//测试
        if (logic.equals(">"))
        {
            return outValue > value;
        }
        else if (logic.equals("<"))
        {
            return outValue < value;
        }
        else if (logic.equals("="))
        {
            return outValue == value;
        }
        else if (logic.equals(">="))
        {
            return outValue >= value;
        }
        else if (logic.equals("<="))
        {
            return outValue <= value;
        }
        else
        {
            return false;
        }
    }

    void print()
    {
        // printStr(id);
        // printStr(logic);
        // printStr(sensorValue);
        printStr(String(this->triggleValue));
        printStr("triggleValue=====", this->triggleValue);
    }
    Conds(/* args */)
    {
    }
    ~Conds()
    {
    }
};

class CondNotice
{
private:
    int count = 0;
    Conds conds[MAX_CONDS_NUM]; // 所有条件都满足才触发
    int frequency = 0;          // 单位秒  -1 永不报警 0 仅一次  其他：分钟数
    bool disable = false;
    long lastTime = 0;
    String name;
    String noticeId;
    bool isSend = false;

public:

    bool isEmpty(){
        return this->name.isEmpty();
    }
    int isNeedTriggle();
    int getCount();
    bool isTriggle();
    String getName();
    String getNoticeId();
    void setName(String name);
    void setNoticeId(String id);
    void print();
    void addCond(Conds cond);
    Conds getCond(int index);
    Conds *getCondById(String id);
    void setFrequency(int frequency);
    void setDisable(bool b);
    bool getDisable();
    CondNotice(/* args */)
    {
    }
    ~CondNotice()
    {
    }
};

class ReqNotice
{
private:
    String deviceId;
    String noticeId;
    String name; // 如果是系统通知，需要指定一个name,而不需要指定noticeId
    String msg;
    String type; // 1,系统通知，2，条件通知，3，动作消息
    long epoch = 0;
    int frequncy = 5; // 单位分钟

public:
    static ReqNotice getReqNotice(String name)
    {

        String json = getPreferenceString(name);
        ReqNotice notice;

        if (json.isEmpty())
        {
            return notice;
        }
        JsonDocument doc;
        DeserializationError error1 = deserializeJson(doc, json);
        if (error1)
        {
            Serial.print(F("deserializeJson() failed: "));
            Serial.println(error1.f_str());
            return notice;
        }
        notice.setDeviceId(doc["deviceId"]);
        notice.setEpoch(doc["epoch"]);
        notice.setFrequency(doc["frequency"]);
        notice.setMsg(doc["msg"]);
        notice.setName(doc["name"]);
        notice.setNoticeId(doc["noticeId"]);
        notice.setType(doc["type"]);
        return notice;
    }

    bool isEmpty()
    {
        if (name.isEmpty())
        {
            return true;
        }

        return false;
    }

    void savePrefrence()
    {
        StaticJsonDocument<200> doc1;            // 分配200字节的静态内存给JSON文档
        JsonObject data = doc1.to<JsonObject>(); // 获取JsonObject
        data["deviceId"] = deviceId;
        data["epoch"] = epoch;
        data["frequency"] = frequncy;
        data["msg"] = msg;
        data["name"] = name;
        data["noticeId"] = noticeId;
        data["type"] = type;
        String jsonString;
        serializeJson(data, jsonString);
        putPreferenceString(name, jsonString);
    }

    void print()
    {
        printStr(name);
        printStr(msg);
    }

    void setDeviceId(String deviceId)
    {
        this->deviceId = deviceId;
    }

    String getDeviceId()
    {
        return this->deviceId;
    }

    void setNoticeId(String noticeId)
    {
        this->noticeId = noticeId;
    }

    String getNoticeId()
    {
        return this->noticeId;
    }

    void setName(String name)
    {
        this->name = name;
    }

    String getName()
    {

        return this->name;
    }

    void setMsg(String msg)
    {
        this->msg = msg;
        if (frequncy > 0)
        {
            String frequncyStr = String(frequncy);
            this->msg.concat("(" + frequncyStr + "分钟内不再发送此类通知)");
        }
    }
    String getMsg()
    {
        return this->msg;
    }

    void setType(String type)
    {
        this->type = type;
    }

    String getType()
    {
        return this->type;
    }

    void setEpoch(long epoch)
    {
        this->epoch = epoch;
    }

    long getEpoch()
    {
        return this->epoch;
    }

    // 必须在setMsg之前调用，setMsg 依赖frequency
    void setFrequency(long frequency)
    {
        this->frequncy = frequency;
    }

    long getFrequecy()
    {
        return this->frequncy;
    }

    bool equals(ReqNotice &notice)
    {
        if (notice.deviceId.equals(this->deviceId) && notice.noticeId.equals(this->noticeId) && notice.name.equals(this->name) && notice.type.equals(this->type))
        {
            return true;
        }

        return false;
    }

    ReqNotice(/* args */)
    {
    }
    ~ReqNotice()
    {
    }
};

bool parseCondNotice(String condNotice);
List<CondNotice> *getCondNoticeList();
void checkSendNotify();

void setupCondNotice();
void initLatestNoticeMap();

ReqNotice getLatestReqNotice(String name);

void putLatestReqNotice(ReqNotice notice);

// 如果有通知就发送通知，没有就不发送，内部判断，此方法必须在loop 或setup中执行
void sendNotice();

void addNotice(ReqNotice req);

void addNotice(String name, String msg, String noticeId, String type, int frequency);

void addOverloadNotice();
void addPumpIdleNotice();
void addLackWaterNotice();
void addLowPowerNotice(String);
#endif
